package com.fyzk.activity.ssc;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.shuzicai.BallInfo;
import com.fyzk.activity.zoushitu.MyLinearLayout;
import com.tyjc.lottery.R;
import com.fyzk.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

public class SSC_DrawView extends LinearLayout{
	
	private Context context;
	private MyLinearLayout view1;
	private int pmW,pmH,pmD1;
	private float pmD2;
	private int qiu_w;
	
	private TextView tv;
	
	private int xing;
	private int wanfa;
	
	private List<Rect> list_rect = new ArrayList<Rect>();
	private List<Rect> list_rect_yl = new ArrayList<Rect>();
	private List<BallInfo> list_qiu = new ArrayList<BallInfo>();
	private List<Integer> list_yl = new ArrayList<Integer>();
	
	private Bitmap qiu_bg_p,qiu_bg_n;
	
	private boolean isXuanQiu = false;
	
	
	public SSC_DrawView(Context context) {
		this(context, null);
	}

	public SSC_DrawView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setFocusable(true);
		
		this.pmW = AppUtil.getWidth();
		this.pmH = AppUtil.getHeight();
		this.pmD1 = AppUtil.getDensityDpi();
		this.pmD2 = AppUtil.getDensity();
		
		qiu_w = (pmW - 6)/10;
		
		this.xing = ShiShiCai.STA_XING1;
		this.wanfa = ShiShiCai.STA_WF1;
		this.context = context;
		
		view1 = new MyLinearLayout(this.context);
		view1.setRL(500, 500);
		view1.setVisibility(View.INVISIBLE);
		this.addView(view1);
		
		
		qiu_bg_p = BitmapFactory.decodeResource(context.getResources(), R.drawable.hongqiu_check);
		qiu_bg_n = BitmapFactory.decodeResource(context.getResources(), R.drawable.qiu_bg);
		
		addQiu(10);//一星时为0~9  共10个球
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Log.e("时时彩   画球区", ".............  pmD1:"+pmD1+"  pmD2"+pmD2+"  pmW:"+pmW+"  pmH:"+pmH);
		
		Rect rect;
		Rect rect_text;
		int jianJu = qiu_w/2;//两排球之间的间距
		int title = qiu_w*2/3;//个位、十位、百位等文字
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		Paint paint_qiu = new Paint(Paint.ANTI_ALIAS_FLAG);
		Paint paint_yl = new Paint(Paint.ANTI_ALIAS_FLAG);
		
		paint.setColor(Color.BLACK);
		paint.setTextSize(16*pmD2);
		
		paint_qiu.setTextSize(14*pmD2);
		paint_qiu.setTextAlign(Align.CENTER);
		Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
		paint_qiu.setTypeface( font );
		
		paint_yl.setTextSize(14*pmD2);
		paint_yl.setTextAlign(Align.CENTER);
		
		
		rect_text = new Rect(3, 3, pmW-3, title);
		
		int qiu_top = rect_text.bottom;
		
		for (int i = 0; i < list_qiu.size(); i++) {
			
			int left = 3+(i%10)*qiu_w;
			int right = 3+((i%10)+1)*qiu_w;
			int top = qiu_top+(qiu_w + jianJu + title)*(i/10);
			int bottom = (qiu_top+qiu_w)+(qiu_w + jianJu + title)*(i/10);
			
			//文字
			switch (xing) {
			case ShiShiCai.STA_XING1:
				if(i == 0){
					rect_text = new Rect(3, top - title, pmW-3, top);
					canvas.drawText("请选择投注号码", 10, rect_text.centerY()+5, paint);
				}
				break;
			case ShiShiCai.STA_XING2:
				switch (wanfa) {
				case ShiShiCai.STA_WF1:
					if(i == 0){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("十位：", 10, rect_text.centerY()+5, paint);
					}else if(i == 10){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("个位：", 10, rect_text.centerY()+5, paint);
					}
					break;
				case ShiShiCai.STA_WF2:
					if(i == 0){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("请选择投注号码", 10, rect_text.centerY()+5, paint);
					}
					break;
				case ShiShiCai.STA_WF3:
					if(i == 0){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("请选择投注号码", 10, rect_text.centerY()+5, paint);
					}
					title = 0;
					top = qiu_top+(qiu_w + jianJu + title)*(i/10);
					bottom = (qiu_top+qiu_w)+(qiu_w + jianJu + title)*(i/10);
					break;
				default:
					break;
				}
				break;
			case ShiShiCai.STA_XING3:
				switch (wanfa) {
				case ShiShiCai.STA_WF1:
					if(i == 0){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("百位：", 10, rect_text.centerY()+5, paint);
					}else if(i == 10){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("十位：", 10, rect_text.centerY()+5, paint);
					}else if(i == 20){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("个位：", 10, rect_text.centerY()+5, paint);
					}
					break;
				case ShiShiCai.STA_WF2:
					if(i == 0){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("请选择投注号码", 10, rect_text.centerY()+5, paint);
					}
					break;
				case ShiShiCai.STA_WF3:
					if(i == 0){
						rect_text = new Rect(3, top - title, pmW-3, top);
						canvas.drawText("请选择投注号码", 10, rect_text.centerY()+5, paint);
					}
					break;
				default:
					break;
				}
				break;
			case ShiShiCai.STA_XING5:
				switch (i) {
				case 0:
					rect_text = new Rect(3, top - title, pmW-3, top);
					canvas.drawText("万位：", 10, rect_text.centerY()+5, paint);
					break;
				case 10:
					rect_text = new Rect(3, top - title, pmW-3, top);
					canvas.drawText("千位：", 10, rect_text.centerY()+5, paint);
					break;
				case 20:
					rect_text = new Rect(3, top - title, pmW-3, top);
					canvas.drawText("百位：", 10, rect_text.centerY()+5, paint);
					break;
				case 30:
					rect_text = new Rect(3, top - title, pmW-3, top);
					canvas.drawText("十位：", 10, rect_text.centerY()+5, paint);
					break;
				case 40:
					rect_text = new Rect(3, top - title, pmW-3, top);
					canvas.drawText("个位：", 10, rect_text.centerY()+5, paint);
					break;
				default:
					break;
				}
				break;
			case ShiShiCai.STA_DAXIAO:
				if(i == 0){
					rect_text = new Rect(3, top - title, pmW-3, top);
					canvas.drawText("请选择投注号码", 10, rect_text.centerY()+5, paint);
				}
				break;
			default:
				break;
			}
			
			
			//画球
			rect = new Rect(left, top, right, bottom);
			
			list_rect.add(rect);
			
			if(list_qiu.get(i).isSelected()){
				paint_qiu.setColor(Color.WHITE);
				canvas.drawBitmap(qiu_bg_p, null, rect, paint);
				
			}else{
				paint_qiu.setColor(Color.BLACK);
				canvas.drawBitmap(qiu_bg_n, null, rect, paint);
			}
			
			drawText(canvas, paint_qiu, ""+list_qiu.get(i).getNum(), rect);
			
			
			//遗漏
			Rect rt = new Rect(rect.left, rect.bottom, rect.right, rect.bottom+jianJu);
			list_rect_yl.add(rt);
			
			int yl = list_yl.get(i);
			if(yl > 60){
				paint_yl.setColor(Color.RED);
			}else{
				paint_yl.setColor(Color.GRAY);
			}
			drawText(canvas, paint_yl, ""+yl, rt);
			
		}
		
		
	}
	
	/**
	 * 触屏事件
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		int x,y;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			x = (int)event.getX();
			y = (int)event.getY();
			
			for (Rect r : list_rect) {
				if(r.contains(x, y)){
					isXuanQiu = true;
					break;
				}
			}
			break;
		case MotionEvent.ACTION_MOVE:
			
			break;
		case MotionEvent.ACTION_UP:
			x = (int)event.getX();
			y = (int)event.getY();
			if(isXuanQiu){
				for (int i = 0; i < list_rect.size(); i++) {
					if(list_rect.get(i).contains(x, y)){
						
						if(list_qiu.get(i).isSelected()){
							list_qiu.get(i).setSelected(false);
						}else{
							list_qiu.get(i).setSelected(true);
						}
						
						this.invalidate();
						yiXuan();
						break;
					}
				}
			}
			isXuanQiu = false;
			
			
			
			break;
			
		default:
			break;
		}
		return true;
	}
	
	/**
	 * 绘制文字
	 * @param canvas
	 * @param paint
	 * @param text
	 * @param rect
	 */
	private void drawText(Canvas canvas,Paint paint,String text,Rect rect){
		FontMetrics fm = paint.getFontMetrics();
		int baseLine = (int)(rect.top + (rect.bottom - rect.top - fm.bottom + fm.top) / 2 - fm.top);  
		canvas.drawText(text, rect.centerX(), baseLine, paint);
	}
	
	/**
	 * 添加球
	 * @param i	球的个数
	 */
	private void addQiu(int i){
		list_qiu.clear();
		list_rect.clear();
		list_rect_yl.clear();
		for (int j = 0; j < i; j++) {
			if(xing == ShiShiCai.STA_XING2 && wanfa == ShiShiCai.STA_WF3){
				BallInfo ssqQiu = new BallInfo(""+j);
				list_qiu.add(ssqQiu);
			}else{
				
				BallInfo ssqQiu = new BallInfo(""+j%10);
				list_qiu.add(ssqQiu);
			}
			
			list_yl.add((int)(Math.random()*100));
		}
	}
	
	/**
	 * 设置画球区宽高
	 * @param w
	 * @param h
	 */
	public void setWH(int w,int h){
		this.removeAllViews();
		view1 = new MyLinearLayout(this.context);
		view1.setRL(w, h);
		view1.setVisibility(View.INVISIBLE);
		this.addView(view1);
	}

	/**
	 * 设置球数
	 */
	public void setQiuShu(){
		switch (xing) {
		case ShiShiCai.STA_XING1:
			addQiu(10);
			setWH(pmW, qiu_w*3);
			break;
		case ShiShiCai.STA_XING2:
			switch (wanfa) {
			case ShiShiCai.STA_WF1:
				addQiu(20);
				setWH(pmW, qiu_w*5);
				break;
			case ShiShiCai.STA_WF2:
				addQiu(10);
				setWH(pmW, qiu_w*3);
				break;
			case ShiShiCai.STA_WF3:
				addQiu(18);
				setWH(pmW, qiu_w*4);
				break;
			default:
				break;
			}
			break;
		case ShiShiCai.STA_XING3:
			switch (wanfa) {
			case ShiShiCai.STA_WF1:
				addQiu(30);
				setWH(pmW, qiu_w*6);
				break;
			case ShiShiCai.STA_WF2:
				addQiu(10);
				setWH(pmW, qiu_w*3);
				break;
			case ShiShiCai.STA_WF3:
				addQiu(10);
				setWH(pmW, qiu_w*3);
				break;
			default:
				break;
			}
			break;
		case ShiShiCai.STA_XING5:
			addQiu(50);
			setWH(pmW, qiu_w*12);
			break;
		case ShiShiCai.STA_DAXIAO:
			addQiu(8);
			setWH(pmW, qiu_w*3);
			break;
		default:
			break;
		}
		
		this.invalidate();
	}

	/**
	 * 已选号码
	 */
	private void yiXuan(){
		String str = "";
		for (BallInfo ssq : list_qiu) {
			if(ssq.isSelected()){
				str+=ssq.getNum()+",";
			}
		}
		if(str.length() > 0){
			
			str.substring(0, str.length()-1);
		}
		
		tv.setText(str);
	}
	
	
	
	public int getXing() {
		return xing;
	}

	public void setXing(int xing) {
		
		this.xing = xing;
		setQiuShu();
	}

	public int getWanfa() {
		return wanfa;
	}

	public void setWanfa(int wanfa) {
		this.wanfa = wanfa;
		setQiuShu();
	}

	public TextView getTv() {
		return tv;
	}

	public void setTv(TextView tv) {
		this.tv = tv;
	}
	
	

}
